<?php
//自动生成,请根据需要修改
namespace app\system\controller;

use app\common\controller\Base;
use think\Db;
use think\db\Where;

/**
 * @title 友情链接页面（模块08-01）
 * @description 友情链接说明
 * @group 公共配置（08）
 * @header name:model_id require:0 default:08-01 desc:模块
 * @groupremark 已使用错误码：1-08-01-0001 校验失败 1-08-01-0002 添加失败 1-08-01-0003 编辑校验失败 1-08-01-0004 编辑失败 1-08-01-0005 获取详情失败  1-08-01-0006 获取列表失败 1-08-01-0007获取单个详情失败 1-08-01-0008 删除失败
 */
class FriendlyLink extends Base
{
    //测试
    public function test()
    {
        return 'test 111';
    }

    /**
     * @title 添加友情链接
     * @description 添加友情链接
     * @author 李锐志
     * @url /system/friendly_link/add
     * @method *
     *
     * @header name:token require:1 default: desc:token
     *
     * @param name:name type:varchar require:0 other: desc:名称
     * @param name:link type:varchar require:0 other: desc:网址
     * @param name:status type:tinyint require:0 other: desc:状态
     * @return data:友情链接@!
     * @data    id:id    name:名称    link:网址    status:状态
     * @return error_id:1-08-01-0001 校验失败  1-08-01-0002 添加失败
     */
    public function add()
    {
        $data = input();
        /**
         *$validate = new \app\system\validate\FriendlyLink();
         *$validate->scene('add');
         *$result = $validate->batch(true)->check($data);
         *if ($result !== true) {
         *$msg['msg']='校验失败';
         *$msg['validate']=$validate->getError();
         *$msg['error_id']='1-08-01-0001';
         *return $this->errorJson($msg);
         *}
         **/
        $model = new \app\system\model\FriendlyLink();
        $flag = $model->save($data);
        if ($flag) {
            $msg['msg'] = '添加成功';
            $msg['data'] = $model->get($model['id']);
            return $this->successJson($msg);
        } else {
            $msg['error_id'] = '1-08-01-0002';
            $msg['msg'] = '添加失败';
            return $this->errorJson($msg);
        }
    }

    /**
     * @title 编辑友情链接
     * @description 编辑友情链接
     * @author 李锐志
     * @url /system/friendly_link/edit
     * @method *
     *
     * @header name:token require:1 default: desc:token
     *
     * @param name:id type:int require:0 other: desc:id
     * @param name:name type:varchar require:0 other: desc:名称
     * @param name:link type:varchar require:0 other: desc:网址
     * @param name:status type:tinyint require:0 other: desc:状态
     * @return data:友情链接@!
     * @data    id:id    name:名称    link:网址    status:状态
     * @return error_id:1-08-01-0003 校验失败 1-08-01-0004 修改失败
     */
    public function edit()
    {
        $data = input();
        $validate = new \app\system\validate\FriendlyLink();
        $validate->scene('edit');
        $result = $validate->batch(true)->check($data);
        if ($result !== true) {
            $msg['msg'] = '校验失败';
            $msg['validate'] = $validate->getError();
            $msg['error_id'] = '1-08-01-0003';
            return $this->errorJson($msg);
        }
        $model = new \app\system\model\FriendlyLink();
        $flag = $model->edit($data);
        if ($flag !== false) {
            $msg['msg'] = '修改成功';
            $msg['data'] = $model->get($data['id']);
            return $this->successJson($msg);
        } else {
            $msg['error_id'] = '1-08-01-0004';
            $msg['msg'] = '修改失败';
            return $this->errorJson($msg);
        }
    }

    /**
     * @title 删除友情链接
     * @description 删除友情链接
     * @author 李锐志
     * @url /system/friendly_link/delete
     * @method *
     *
     * @param name:token require:1 default: desc:token
     *
     * @param name:id type:int|array require:1 other: desc:要删除的id
     * @return error_id:1-08-01-0008 删除失败
     */
    public function delete()
    {
        $id = input('id');
        $model = new \app\system\model\FriendlyLink();
        $flag = $model->destroy($id);
        if ($flag) {
            $msg['msg'] = '删除成功';
            return $this->successJson($msg);
        } else {
            $msg['msg'] = '删除失败';
            $msg['error_id'] = '1-08-01-0008';
            return $this->errorJson($msg);
        }
    }

    /**
     * @title 单个查询友情链接
     * @description 单个查询友情链接
     * @author 李锐志
     * @url /system/friendly_link/info
     * @method *
     *
     * @param name:token require:1 default: desc:token
     *
     * @param name:id type:int require:0 other: desc:id
     * @param name:name type:varchar require:0 other: desc:名称
     * @param name:link type:varchar require:0 other: desc:网址
     * @param name:status type:tinyint require:0 other: desc:状态
     * @return data:友情链接@!
     * @data    id:id    name:名称    link:网址    status:状态
     * @return error_id:1-08-01-0005 校验失败 1-08-01-0007 获取失败
     */
    public function info()
    {
        $model = new \app\system\model\FriendlyLink();
        $Fields = $model->getTableFields();
        $data = \think\facade\Request::only($Fields);
        /**
         *$validate = new \app\system\validate\FriendlyLink();
         *$validate->scene('info');
         *$result = $validate->batch(true)->check($data);
         */
        if (!$data) {
            $msg['msg'] = '必须有参数';
            $msg['error_id'] = '1-08-01-0005';
            return $this->errorJson($msg);
        }
        $model = new \app\system\model\FriendlyLink();
        $info = $model->where($data)->find();
        if ($info) {
            $msg['msg'] = '获取成功';
            $msg['data'] = $info;
            return $this->successJson($msg);
        } else {

            $msg['msg'] = '获取失败';
            $msg['error_id'] = '1-08-01-0007';
            return $this->errorJson($msg);
        }
    }

    /**
     * @title 查询列表友情链接
     * @description 查询列表友情链接
     * @author 李锐志
     * @url /system/friendly_link/get_list
     * @method *
     * @param name:token require:1 default: desc:token
     * @param name:page require:1 default: desc:第几页
     * @param name:pagesize require:1 default: desc:每页数
     * @return list:友情链接@
     * @list    id:id    name:名称    link:网址    status:状态
     * @return error_id:1-08-01-0006 校验失败
     */
    public function get_list()
    {
        $model = new \app\system\model\FriendlyLink();
        $Fields = $model->getTableFields();
        $data = \think\facade\Request::only($Fields);
        foreach($data as $k => $v){
            if($v === 0){
                continue;
            }
            if($v == null){
                unset($data[$k]);
            }
        }
        if(isset($data['name']) && $data['name'] && !empty($data['name'])){
            $data['name'] = ['like',"%{$data['name']}%"];
        }
        if(isset($data['status']) && $data['status'] && !empty($data['status'])){
            $data['status'] = ['=',"{$data['status']}"];
        }
        /**
         *$validate = new \app\system\validate\FriendlyLink();
         *$validate->scene('lists');
         *$result = $validate->batch(true)->check($data);
         *if ($result !== true) {
         *$msg['msg']='校验失败';
         *$msg['validate']=$validate->getError();
         *$msg['error_id']='1-08-01-0006';
         *return $this->errorJson($msg);
         *}
         **/
        $data = new Where($data);
        $list = $model->where($data)->paginate();
        $msg['list'] = $list;
        return $this->successJson($msg);
    }

    /**
     * @title 前台获取友情链接
     * @description 前台获取友情链接
     * @author 李锐志
     * @url /system/friendly_link/get_friendly_link
     * @method *
     */
    public function get_friendly_link()
    {
        $result = Db::name('friendly_link')
            ->where('status',1)
            ->select();

        $msg['msg'] = '友情链接获取成功';
        $msg['data'] = $result;
        return $this->successJson($msg);
    }
    /**
     * 后台站内信已读按钮
     */
    public function invoke()
    {
        $data = input();

        Db::name('friendly_link')
            ->where('id',$data['id'])
            ->setField('status',1);

        $msg['msg'] = '站内信已读成功';
        return $this->successJson($msg);
    }

    /**
     * 后台站内信未读按钮
     */
    public function forbidden()
    {
        $data = input();

        Db::name('friendly_link')
            ->where('id',$data['id'])
            ->setField('status',0);

        $msg['msg'] = '站内信未读成功';
        return $this->successJson($msg);
    }
}
